package com.zsl.picture.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsl.picture.model.Photo;
import com.zsl.picture.service.MessagesService;
import com.zsl.picture.service.PhotoService;
import com.zsl.picture.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 存放照片 前端控制器
 * </p>
 *
 * @author zsl
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/photo")
public class PhotoController {
    @Autowired
    private PhotoService photoService;

    @Autowired
    private MessagesService messagesService;

    @GetMapping
    public R list(@ModelAttribute Photo photo){
        IPage<Photo> page = photoService.queryPage(photo);
        List<Photo> list = page.getRecords();
        return R.ok().put("list",list)
                .put("pageNo",page.getCurrent())
                .put("pageSize",page.getSize());
    }

    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public R post(@RequestBody Photo photo){
        photo.setCreateTime(new Date());
        photoService.save(photo);
        if(photo.getId()==null){
            return R.error("保存失败");
        }
        return R.ok("上传成功").put("photo",photo);
    }

}
